package com.dygg.bigfish.controller;

import com.alibaba.fastjson2.JSON;
import com.dygg.bigfish.dao.InvitationDao;
import com.dygg.bigfish.entity.Invitation;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "invitationServlet", value = "/invitationServlet")
public class InvitationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if ("select".equals(op)) {
            Select(request, response);
        } else if ("post".equals(op)) {
            Post(request, response);
        } else if ("delete".equals(op)) {
            delete(request, response);
        } else if ("reply".equals(op)) {
            reply(request, response);
            changeToMark(request, response);
        } else if ("changeToMark".equals(op)) {
            changeToMark(request, response);
        }

    }

    //查询所有数据
    protected void Select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从数据库中获取数据
        InvitationDao invitationDao = new InvitationDao();

        List<Invitation> invitations = invitationDao.queryAllInvitations();
        String s = JSON.toJSONString(invitations);
        PrintWriter writer = response.getWriter();

        writer.println(s);
        writer.flush();
        writer.close();
    }

    //发帖
    protected void Post(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String subjectInput = request.getParameter("subjectInput");//获取subjectInput(帖子标题)参数
        String contentInput = request.getParameter("contentInput");//获取contentInput(帖子内容)参数
        Integer userid = Integer.parseInt(request.getParameter("id"));//获取id(用户id)参数

        //从数据库中获取数据
        InvitationDao invitationDao = new InvitationDao();
        //具体需要调用的后面再加
        String id = invitationDao.getSystemId();
        String result = "";
        if (userid != null) {
            boolean flag1 = invitationDao.InsertPost(id, subjectInput, userid);
            boolean flag2 = invitationDao.addPost(id, contentInput, userid);
            if (flag1 && flag2) {
                result = "true";
            } else {
                result = "false";
            }
        } else {
            result = "false";
        }
        PrintWriter writer = response.getWriter();

        writer.println(result);
        writer.flush();
        writer.close();

    }

    //回帖
    protected void reply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Invitationid = request.getParameter("Invitation_id"); //获取帖子id参数
        String contentInput = request.getParameter("contentInput");//获取contentInput(帖子内容)参数
        Integer userid = Integer.parseInt(request.getParameter("id"));//获取id(用户id)参数

        //从数据库中获取数据
        InvitationDao invitationDao = new InvitationDao();
        String result = "";
        if (userid != null) {
            boolean flag1 = invitationDao.modifyPost(Invitationid, userid);
            boolean flag2 = invitationDao.addPost(Invitationid, contentInput, userid);
            if (flag1 && flag2) {
                result = "true";
            } else {
                result = "false";
            }
        } else {
            result = "false";
        }
        PrintWriter writer = response.getWriter();

        writer.println(result);
        writer.flush();
        writer.close();
    }

    //删帖
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sID = request.getParameter("subjectID");//获取subjectID(帖子ID)参数
        String cID = request.getParameter("contentID");//contentID(帖子楼层ID)参数
        //从数据库中获取数据
        InvitationDao invitationDao = new InvitationDao();
        //具体需要调用的后面再加
        String result = "";
        boolean flag1 = false;
        boolean flag2 = false;
        boolean flag3 = false;
        if (cID != null) {
            flag1 = true;
            flag2 = invitationDao.deletePostContent(sID, cID);
            flag3 = invitationDao.contentCountDown(sID);
        } else {
            flag1 = invitationDao.deletePost(sID);
            flag2 = invitationDao.deletePostContent(sID, "");
            flag3 = true;
        }
        if (flag1 && flag2 && flag3) {
            result = "true";
        } else {
            result = "false";
        }

        PrintWriter writer = response.getWriter();

        writer.println(result);
        writer.flush();
        writer.close();
    }

    //设置各种标记/取消
    //topMark 置顶帖标志
    //primeMark 精华帖标志
    //lockMark 锁定标志
    //reversalMark 反向显示标志
    // (0=false，1=true)
    protected void changeToMark(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Invitationid = request.getParameter("Invitation_id"); //获取帖子id参数
        Integer userid = Integer.parseInt(request.getParameter("id"));//获取id(用户id)参数
        String topMark = request.getParameter("topMark");//获取置顶帖参数
        String primeMark = request.getParameter("primeMark");//获取置顶帖参数
        String lockMark = request.getParameter("lockMark");//获取置顶帖参数
        String reversalMark = request.getParameter("reversalMark");//获取置顶帖参数

        //从数据库中获取数据
        InvitationDao invitationDao = new InvitationDao();
        String result = "";
        if (userid != null
                && (topMark == "0" || topMark == "1")
                && (primeMark == "0" || primeMark == "1")
                && (lockMark == "0" || lockMark == "1")
                && (reversalMark == "0" || reversalMark == "1")) {
            boolean flag = invitationDao.changeToMark(Invitationid, topMark, primeMark, lockMark, reversalMark);
            if (flag) {
                result = "true";
            } else {
                result = "false";
            }
        } else {
            result = "false";
        }
        PrintWriter writer = response.getWriter();

        writer.println(result);
        writer.flush();
        writer.close();
    }

}
